<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            二维码盘点
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">资产编码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="输入/扫描资产编码" id="rsCode"  />
                    </div>
                </div>
            </li>
        </ul>
        <p></p>
        <p></p>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="saveQrcode()">保存</div>

    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    var chkBillType = ""; //检测单类型
    //初始化
    apiready = function() {

        $("#rsCode").focus();

    }
    //二维码扫描监听回车事件
    $("#rsCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var rsCode = $(this).val();
        if (keycode == '13') {

            //发货单
            $("#rsCode").val(rsCode);

            //查询表格数据
            saveQrcode();
        }
    });



    //二维码扫码保存
    function saveQrcode() {
        //操作状态
        var type = api.pageParam.type;
        if (type == 'OnLine') {
            OnLine();
        } else if (type == 'OffLine') {
            OffLine();
        }

    }
    function OnLine() {
        //盘点单号
        var billCode = api.pageParam.billCode;
        //资产编码
        var rsCode = $("#rsCode").val();
        //盘点单号非空校验
        if (billCode == "" || billCode == null || billCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }
        //资产编码非空校验
        if (rsCode == "" || rsCode == null || rsCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }
        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveQrcode";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                billCode: billCode,
                rsCode: rsCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    //清空表单
                    $("#rsCode").val("");
                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }
    function OffLine() {
        //盘点单号
        var billCode = api.pageParam.billCode;
        //资产编码
        var rsCode = $("#rsCode").val();
        //盘点单号非空校验
        if (billCode == "" || billCode == null || billCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }
        //资产编码非空校验
        if (rsCode == "" || rsCode == null || rsCode == undefined) {
            api.toast({
                msg: '盘点单号不能为空',
                duration: 3000
            });
            return;
        }
        var db = api.require('db');
        db.openDatabase({
            name: 'test'
        }, function (ret, err) {
            if (ret.status) {
                console.log("1：数据库创建成功");

                //创建数据表
                db.executeSql({
                    name: 'test',
                    sql: 'CREATE TABLE if not exists AssetEncoding( ZcCode varchar(255),billCode varchar(255))'
                }, function (ret, err) {
                    if (ret.status) {

                        console.log("2：数据库表AssetEncoding创建成功");

                        //插入数据
                        db.executeSql({
                            name: 'test',

                            sql: " insert into AssetEncoding(ZcCode,billCode) values('" + rsCode + "','" + billCode + "')"


                        }, function (ret, err) {
                            if (ret.status) {
                                //清空表单
                                 $("#rsCode").val("");
                                api.hideProgress();
                                api.toast({
                                    msg: "数据已存至本地数据库",
                                    duration: 3000
                                });
                                console.log("3：数据库表数据插入成功");

                            } else {
                                alert(JSON.stringify(err));
                            }
                        });

                    } else {
                        alert(JSON.stringify(err));
                    }
                });
            } else {
                alert(JSON.stringify(err));
            }
        });
    }
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>